"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 人为因素
Case Name   : TPCC压力下，DN备所在机器重启
Create At   : 2021/11/19
@zou_jialiang0505328126
Description :
    1、利用benchmarksql工具运行TPCC
    2、DN备所在机器重启reboot
    3、单独启备库 gs_ctl start -D [dir] -M Standby
    4、进行dml操作，检查主备一致性
Expect      :
    1、TPCC运行正常
    2、重启成功，TPCC正常运行
    3、DN备状态恢复正常
    4、dml操作成功，主备一致
History     :
"""

import unittest
import time
from yat.test import Node, macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class RebootAndRunTPCC(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Human_Case008:开始执行---------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.sta1_root_node = Node(node='Standby1Root')
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta2_dbuser = Node(node='Standby2DbUser')
        self.com = Common()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.constant = Constant()

    def test_main(self):
        text1 = '----step1: 利用benchmarksql工具运行TPCC expect:TPCC运行正常----'
        self.log.info(text1)
        tpcc_thread = ComThread(
            self.com.startTPCC,
            args=(self.primary_root_node, macro.TPCC_PATH))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()

        text = '----step2: DN备所在机器重启reboot expect:重启成功，TPCC正常运行----'
        self.log.info(text)
        result = self.sta1_root_node.sh('reboot').result()
        self.log.info(result)

        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result,
                      '执行失败:' + text1)

        text = '----step3: 单独启备库 expect:重启成功----'
        time.sleep(180)
        self.log.info(text)
        for i in range(1, 65):
            start_result = self.sta_1_sh.startDbInstance('standby')
            self.log.info(start_result)
            time.sleep(10)
            result = self.primary_sh.getDbClusterStatus('status')
            if result:
                break
            else:
                time.sleep(60)
        self.assertTrue(result, '执行失败:' + text)

        text = '----step4: 进行dml操作，检查主备一致性 expect:主备一致----'
        self.log.info(text)
        result = self.sta_1_sh.check_data_consistency()
        self.assertTrue(result, '执行失败:' + text)
        result = self.sta_2_sh.check_data_consistency()
        self.assertTrue(result, '执行失败:' + text)
        nodes_tuple = (self.primary_user_node,
                       self.sta1_dbuser, self.sta2_dbuser)
        flag = self.com.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag)

        table_dict = self.com.format_sql_result(
            self.primary_sh.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.com.check_data_sample_by_all(select_sql,
                                                     *nodes_tuple)
            self.assertTrue(flag, '执行失败:' + text)

    def tearDown(self):
        self.log.info("-------------无需清理环境---------------")
        self.log.info(
            '--------Opengauss_Reliability_Human_Case008:执行完成--------')
